const express = require('express')
const router = require('./router')
const fs = require('fs')
const https = require('https')
const bodyParser = require('body-parser')
const cors = require('cors')

// 创建 express 应用
const app = express()

app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use('/', router)

// 使 express 监听 5000 端口号发起的 http 请求
const privateKey = fs.readFileSync('./https/www.xxlb.site.key', 'utf8')
const pem = fs.readFileSync('./https/www.xxlb.site.pem', 'utf8')
const credentials = { key: privateKey, cert: pem }
const httpsServer = https.createServer(credentials, app)

const server = app.listen(5000, function() {
  const { address, port } = server.address()
  console.log('Http Server is running on http://%s:%s', address, port)
}) 

httpsServer.listen(18082, function() {
  console.log('HTTPS Server is running on: https://localhost:%s', 18082)
})